<?php

/**
 * This is the model class for table "sys_serial_products_buyer".
 *
 * The followings are the available columns in table 'sys_serial_products_buyer':
 * @property string $serial_products_buyer_id
 * @property string $serial_products_buyer_serial_products_id
 * @property string $serial_products_buyer_network_id
 * @property string $serial_products_buyer_datetime
 */
class sys_serial_products_buyer extends CActiveRecord {

    public $member_name;
    public $network_mid;
    public $factory;
    public $warehouse;
    public $distributor;
    public $count_active;
    public $count_pasive;
    public $total_pasive_active;
    public $total_products;
    /**
     * Returns the static model of the specified AR class.
     * @return sys_serial_products_buyer the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'sys_serial_products_buyer';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('serial_products_buyer_serial_products_id', 'length', 'max' => 16),
            array('member_name, serial_products_buyer_network_id', 'length', 'max' => 20),
            array('network_mid, warehouse, factory, distributor', 'length', 'max' => 11),
            array('serial_products_buyer_datetime', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('network_mid, warehouse, factory, distributor, serial_products_buyer_id, serial_products_buyer_serial_products_id, serial_products_buyer_network_id, serial_products_buyer_datetime', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
           // 'rel_serial_products_buyer_to_serial_products' => array(self::HAS_ONE, 'sys_serial_products', 'serial_products_buyer_serial_products_id'),
            'rel_serial_products_buyer_to_serial_products' => array(self::BELONGS_TO, 'sys_serial_products', 'serial_products_buyer_serial_products_id'),
            'rel_serial_products_buyer_to_network' => array(self::BELONGS_TO, 'sys_network', 'serial_products_buyer_network_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'serial_products_buyer_id' => 'Serial Buyer',
            'serial_products_buyer_serial_products_id' => 'Serial Products',
            'serial_products_buyer_network_id' => 'Serial Buyer',
            'serial_products_buyer_datetime' => 'Serial Buy Datetime',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('serial_products_buyer_id', $this->serial_products_buyer_id, true);
        $criteria->compare('serial_products_buyer_serial_products_id', $this->serial_products_buyer_serial_products_id, true);
        $criteria->compare('serial_products_buyer_network_id', $this->serial_products_buyer_network_id, true);
        $criteria->compare('serial_products_buyer_datetime', $this->serial_products_buyer_datetime, true);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }
    
    public function search_serial_report()
    {
        $criteria= new CDbCriteria;
        
        $convert_date=new ConvertDateHelper;   
        
        /*
         * DEFAULT WAKTU 
         * KEMARIN DAN SEKARANG
         */
        
        /*
         * CONSTANT ID
         */
        $factory=4;
        $warehouse=3;
        $distributor=2;
        
        
        $start=date('Y-m-d',  mktime(0, 0, 0, date('m'), date('d')-1, date('Y')));
        $end=date('Y-m-d');
        if(isset($_GET['sys_serial_products_buyer']))
        {
            if(isset($_GET['start_date'],$_GET['end_date']) AND trim($_GET['start_date'])!='' AND trim($_GET['end_date'])!='')
            {
                     $start=$convert_date->explode_datetime_iso('/', $_GET['start_date'], $type="date");
                     $end=$convert_date->explode_datetime_iso('/', $_GET['end_date'], $type="date");
       
            }
        }
        
        //AND sub_sspb.serial_products_buyer_datetime BETWEEN \''.$start.'\' AND \''.$end.'\'
                            
        $criteria->select=' t.*,
            
                            (SELECT COUNT(sub_ssp.serial_products_products_category_id) FROM sys_serial_products_buyer sub_sspb 
                            LEFT JOIN sys_serial_products sub_ssp ON sub_ssp.serial_products_id=sub_sspb.serial_products_buyer_serial_products_id
                            WHERE sub_sspb.serial_products_buyer_network_id=t.serial_products_buyer_network_id
                            AND sub_ssp.serial_products_products_category_id=\''.$warehouse.'\'
                            AND sub_sspb.serial_products_buyer_datetime=t.serial_products_buyer_datetime
                            ) AS warehouse,
                            
                            (SELECT COUNT(sub_ssp.serial_products_products_category_id) FROM sys_serial_products_buyer sub_sspb 
                            LEFT JOIN sys_serial_products sub_ssp ON sub_ssp.serial_products_id=sub_sspb.serial_products_buyer_serial_products_id
                            WHERE sub_sspb.serial_products_buyer_datetime=t.serial_products_buyer_datetime
                            AND sub_sspb.serial_products_buyer_network_id=t.serial_products_buyer_network_id
                            AND sub_ssp.serial_products_products_category_id='.$factory.' 
                            ) AS factory,
                            
                            (SELECT COUNT(sub_ssp.serial_products_products_category_id) FROM sys_serial_products_buyer sub_sspb 
                            LEFT JOIN sys_serial_products sub_ssp ON sub_ssp.serial_products_id=sub_sspb.serial_products_buyer_serial_products_id
                            WHERE sub_sspb.serial_products_buyer_network_id=t.serial_products_buyer_network_id
                            AND sub_ssp.serial_products_products_category_id='.$distributor.' 
                            AND sub_sspb.serial_products_buyer_datetime=t.serial_products_buyer_datetime
                            ) AS distributor,
                          
                            
                            (SELECT COUNT(*) FROM sys_serial_products_buyer sub_sspb 
                            LEFT JOIN sys_serial_products sub_ssp ON sub_ssp.serial_products_id=sub_sspb.serial_products_buyer_serial_products_id
                            WHERE sub_sspb.serial_products_buyer_network_id=t.serial_products_buyer_network_id
                            AND sub_ssp.serial_products_is_active=\'0\' AND sub_ssp.serial_products_is_used=\'0\'
                            AND sub_sspb.serial_products_buyer_datetime=t.serial_products_buyer_datetime
                            ) AS count_pasive,
                            
                            (SELECT COUNT(*) FROM sys_serial_products_buyer sub_sspb 
                            LEFT JOIN sys_serial_products sub_ssp ON sub_ssp.serial_products_id=sub_sspb.serial_products_buyer_serial_products_id
                            WHERE sub_sspb.serial_products_buyer_network_id=t.serial_products_buyer_network_id
                            AND sub_ssp.serial_products_is_active=\'1\' AND sub_ssp.serial_products_is_used=\'1\'
                            AND sub_sspb.serial_products_buyer_datetime=t.serial_products_buyer_datetime
                            ) AS count_active';
        
        $criteria->addBetweenCondition('t.serial_products_buyer_datetime', $start, $end);
        
        $criteria->with=array('rel_serial_products_buyer_to_serial_products','rel_serial_products_buyer_to_network',
                                'rel_serial_products_buyer_to_network.rel_network_to_member',);
        $criteria->together=true;
        $criteria->order='t.serial_products_buyer_datetime DESC';
        $criteria->group='t.serial_products_buyer_network_id, t.serial_products_buyer_datetime';
       
        $criteria->compare('serial_products_buyer_id', $this->serial_products_buyer_id, true);
        $criteria->compare('serial_products_buyer_serial_products_id', $this->serial_products_buyer_serial_products_id, true);
        $criteria->compare('serial_products_buyer_network_id', $this->serial_products_buyer_network_id, true);
        $criteria->compare('serial_products_buyer_datetime', $this->serial_products_buyer_datetime, true);
        $criteria->compare('rel_network_to_member.member_name', $this->member_name, true);
        $criteria->compare('rel_serial_products_buyer_to_network.network_mid', $this->network_mid, true);
        
        $criteria->compare('(SELECT COUNT(*) FROM sys_serial_products_buyer sub_sspb 
                            LEFT JOIN sys_serial_products sub_ssp ON sub_ssp.serial_products_id=sub_sspb.serial_products_buyer_serial_products_id
                            WHERE sub_sspb.serial_products_buyer_network_id=t.serial_products_buyer_network_id
                            AND sub_ssp.serial_products_is_active=\'0\' AND sub_ssp.serial_products_is_used=\'0\'
                            AND sub_sspb.serial_products_buyer_datetime=t.serial_products_buyer_datetime
                            )', $this->count_pasive, false);
        
        $criteria->compare('(SELECT COUNT(*) FROM sys_serial_products_buyer sub_sspb 
                            LEFT JOIN sys_serial_products sub_ssp ON sub_ssp.serial_products_id=sub_sspb.serial_products_buyer_serial_products_id
                            WHERE sub_sspb.serial_products_buyer_network_id=t.serial_products_buyer_network_id
                            AND sub_ssp.serial_products_is_active=\'1\' AND sub_ssp.serial_products_is_used=\'1\'
                            AND sub_sspb.serial_products_buyer_datetime=t.serial_products_buyer_datetime
                            )', $this->count_active, false);
        
        $criteria->compare('(SELECT COUNT(*) FROM sys_serial_products_buyer sub_sspb 
                            LEFT JOIN sys_serial_products sub_ssp ON sub_ssp.serial_products_id=sub_sspb.serial_products_buyer_serial_products_id
                            WHERE sub_sspb.serial_products_buyer_network_id=t.serial_products_buyer_network_id
                            AND sub_ssp.serial_products_products_category_id='.$factory.' 
                            AND sub_sspb.serial_products_buyer_datetime=t.serial_products_buyer_datetime
                            )', $this->factory, false);
        
        $criteria->compare('(SELECT COUNT(*) FROM sys_serial_products_buyer sub_sspb 
                            LEFT JOIN sys_serial_products sub_ssp ON sub_ssp.serial_products_id=sub_sspb.serial_products_buyer_serial_products_id
                            WHERE sub_sspb.serial_products_buyer_network_id=t.serial_products_buyer_network_id
                            AND sub_ssp.serial_products_products_category_id='.$warehouse.' 
                            AND sub_sspb.serial_products_buyer_datetime=t.serial_products_buyer_datetime
                            )', $this->warehouse, false);
        $criteria->compare('(SELECT COUNT(*) FROM sys_serial_products_buyer sub_sspb 
                            LEFT JOIN sys_serial_products sub_ssp ON sub_ssp.serial_products_id=sub_sspb.serial_products_buyer_serial_products_id
                            WHERE sub_sspb.serial_products_buyer_network_id=t.serial_products_buyer_network_id
                            AND sub_ssp.serial_products_products_category_id='.$distributor.' 
                            AND sub_sspb.serial_products_buyer_datetime=t.serial_products_buyer_datetime
                            )', $this->distributor, false);

        
        $data=new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
            
        ));
        $_SESSION['data_export']=$data;
        return $data;
        
    }

}